import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { TUserBasic } from '../../entities/user-basic.entity';

@Injectable()
export class TUserBasicService {
  constructor(
    @InjectRepository(TUserBasic)
    private readonly userRepository: Repository<TUserBasic>,
  ) {}

  /**
   * 查询 t_user_basic 表的所有数据
   */
  async getUserInfo(id: number): Promise<TUserBasic[]> {
    return await this.userRepository.find({
      where: {
        id: id
      }
    });
  }

  /**
   * 查询所有数据并按照创建时间倒序排列
   */
  async findAllOrderByCreateTime(): Promise<TUserBasic[]> {
    return await this.userRepository.find({
      order: {
        createTime: 'DESC', // 按创建时间倒序排列
      },
    });
  }

  /**
   * 查询所有数据，可以选择排序方式
   */
  async findAllWithOptions(
    orderBy: 'ASC' | 'DESC' = 'DESC',
  ): Promise<TUserBasic[]> {
    return await this.userRepository.find({
      order: {
        createTime: orderBy,
      },
    });
  }
}
